package com.example.barnes.ummo.gcm;
/**
* Created by sihle on 12/3/15.
*/
import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.example.barnes.ummo.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import java.io.IOException;
/**
* Created by sihle on 11/28/15.
*/
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public static final String SENT_TOKEN_TO_SERVER = "sentTokenToServer";
public static final String GCM_TOKEN = "gcmToken";
public RegistrationIntentService(){
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG,"Handle Intenet");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String token = "";
InstanceID instanceID = InstanceID.getInstance(this);
String senderId = getResources().getString(R.string.gcm_defaultSenderId);
// Fetch token here
try {
token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
Log.d(TAG, "GCM Registration Token: " + token);
} catch (IOException e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, false).apply();
}
// save token
sharedPreferences.edit().putString(GCM_TOKEN, token).apply();
// pass along this data
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}